//
//  SlideBar.swift
//  Casecord
//
//  Created by baozi on 2024/1/5.
//

import SwiftUI

struct SideBarMenu: View {
    @Binding var selectTab: SideItemType
    @Namespace var animation
    var action: TabActionCallback
    
    var nickName: String {
        get {
            if let name = UserService.shared.current?.user?.nickname {
                let nick = name.replacingOccurrences(of: "honey_", with: "")
                return nick
            }
            return ""
        }
    }
    
    var body: some View {
        VStack(alignment: .leading, spacing: 15) {
            Image.load(UserService.shared.current?.user?.headUrl)
                .resizable()
                .aspectRatio(contentMode: .fill)
                .frame(width: 70, height: 70)
                .cornerRadius(10)
                .paddingTop(60)
                .paddingHorizontal(20)
            Text(nickName)
                .font(.title)
                .fontWeight(.heavy)
                .foregroundColor(.white)
                .paddingTop(10)
                .paddingHorizontal(8)
            VStack {
                TabBtn(image: "house", title: SideItemType.home, selectTab: $selectTab, animation: animation) { type in
                    action(type)
                }
                
                TabBtn(image: "figure.child.and.lock", title: SideItemType.security, selectTab: $selectTab, animation: animation) { type in
                    action(type)
                }
                
                TabBtn(image: "doc.questionmark", title: SideItemType.about, selectTab: $selectTab, animation: animation) { type in
                    action(type)
                }
                
                TabBtn(image: "person", title: SideItemType.help, selectTab: $selectTab, animation: animation) { type in
                    action(type)
                }
            }
            .paddingTop(50)
            Spacer()
            VStack(alignment: .leading, spacing: 6, content: {
                
                TabBtn(image: "rectangle.righthalf.inset.fill.arrow.right", title: SideItemType.logout, selectTab: .constant(.home), animation: animation) { type in
                    UserService.shared.logout()
                }
                .padding(.leading, -15)
                
                Text("App Version 1.2.34")
                    .font(.caption)
                    .fontWeight(.semibold)
                    .foregroundColor(.white)
                    .opacity(0.6)
            })
            .padding()

        }
        .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)
    }
}

#Preview {
    ZStack {
        Color.orange.ignoresSafeArea()
        SideBarMenu(selectTab: .constant(SideItemType.home)) { type in
            print("type: \(type)")
        }
    }
}
